﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32; //FileDialogs
using Microsoft.Windows.Controls.Ribbon;
using System.Windows.Threading; //DSP

using System.Windows.Interop;
using System.Runtime.InteropServices;
using System.Drawing;
using System.IO;
namespace MediaPlayer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
     
    public partial class MainWindow :Window
    {
        
        int playing = 0;                                // 0 not playing , 1 playing,2 paused.
        string pathname = "";
        double oldVolume=0;
        int slider_flag = 0;                            //used to denote whether or not Value Changed Event occurs from Slider or Manual Change.(0 for slider and 1 for Manual Change)
        DispatcherTimer dsp = new DispatcherTimer();
        double original_main_menu_height = 0;           //Retains the original Height of main menu.
        WindowStyle old_wstyle = new WindowStyle();     //Retains the original Window Style.
       bool IsTopmost ;                                 //Retains whether the window was intially a TopMost Window.
        WindowState old_wstate = new WindowState();     //Retains the original Window Style.
        ResizeMode old_rmode = new ResizeMode();        //Retains the original Resize Mode
        double old_grid_height = 0;                     //Retains the original Grid height
        int dbl_click_flag = 0;                         // Whether double click is to shrink or to expand the screen 0 for expand, 1 for shrink 
       // bool loop=false;                              //Loop or Not
        double height;
        double width;
        double screen_aspect_ratio;
        double video_ratio;
        bool full_screen=false;    //0 for no 1 for yes
        bool mute = false;
        double vol = 0;
        string[] playlist={""};
        int playlist_count = 0;
        double old_timeline_height = 0;
        bool playlist_topmost = false;
       
        public MainWindow()
        {
           
          
            InitializeComponent();
        }

        
        private void Window_Unloaded(object sender, RoutedEventArgs e)
        {
            player.Close();
            
        }


        private void player_MediaOpened(object sender, RoutedEventArgs e)
        {
            slider_flag = 0;
            height = System.Windows.SystemParameters.PrimaryScreenHeight;
            width = System.Windows.SystemParameters.PrimaryScreenWidth;
            video_ratio = ((Double)player.NaturalVideoWidth) / (player.NaturalVideoHeight);
            screen_aspect_ratio = width / height;
            playing = 1;
           
            time_Slider.Value=0;
          //  time_Slider.TickFrequency=1;
           // time_Slider.TickPlacement = System.Windows.Controls.Primitives.TickPlacement.BottomRight;
            time_Slider.Maximum = player.NaturalDuration.TimeSpan.TotalSeconds;

            dsp.Interval = new TimeSpan(0, 0,1);
            dsp.Tick += new EventHandler(dsp_Tick);
            dsp.Start();
            
        }

     

        void dsp_Tick(object sender, EventArgs e)
        {
            slider_flag = 1;
            time_Slider.Value += .95;

            time_Slider.SelectionEnd=time_Slider.Value;
            timeline_textBox.Text=player.Position.Hours+"h"+":"+player.Position.Minutes+"m"+":"+player.Position.Seconds +"s";
            if(player.NaturalDuration.HasTimeSpan)
            taskbarItem.ProgressValue=(player.Position.TotalSeconds/(player.NaturalDuration.TimeSpan.TotalSeconds));
            CommandManager.InvalidateRequerySuggested();
            slider_flag = 0;
       }

     

        private void player_MediaEnded(object sender, RoutedEventArgs e)
        {
            playing = 0;
            player.Close();
                time_Slider.Value = 0;
                time_Slider.SelectionEnd = 0;
                dsp.Stop();
              
                    if (!playlist[0].Equals(""))
                    {
                        if (playlist_count < playlist.Length)
                        {
                            pathname = playlist[playlist_count].ToString();
                            player.Source = new Uri(pathname);
                            player.LoadedBehavior = MediaState.Manual;
                            playlist_count++;
                            PlayFile();

                        }
                        else
                        {
                            pathname = "KV Player";
                            main_window.Title = pathname;
                            playlist = new string[1] { "" };
                        }

                    }
              
                
        }
   

      
        private void PlayFile()
        {

            if (playing != 2)
            {
                main_window.Title = pathname;
                time_Slider.Value = 0;
                playing = 1;
                System.Windows.Controls.Image img = new System.Windows.Controls.Image();
                Uri uri = new Uri("Pause.png", UriKind.Relative);
                img.Source = new BitmapImage(uri);


                Button_Play.Content = img;
               //player.Focus();
                player.Play();
            }
            else
            {
                dsp.Start();
                playing = 1;
                System.Windows.Controls.Image img = new System.Windows.Controls.Image();
                Uri uri = new Uri("Pause.png", UriKind.Relative);
                img.Source = new BitmapImage(uri);

                Button_Play.Content = img;

                //player.Focus();
                player.Play();
            }
                        
        }
        private void Open_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "All Files (*.*) |*.*|Video Files (*.AVI;*.MPG;*.MPEG;*.DIVX;*.XVID;*.MKV;*.MP4;*.3GP)|*.AVI;*.MPG;*.MPEG;*.DIVX;*.XVID;*.MKV;*.MP4;*.3GP |Audio Files(*.WAV;*.MP3;*.WMA;*.CDA)|*.WAV;*.MP3;*.WMA;*.CDA";
            ofd.ShowDialog();
            pathname = ofd.FileName;
            if (!pathname.Equals(""))   
            {
                
                    player.Source = new Uri(pathname);

               
                player.LoadedBehavior = MediaState.Manual;
                playlist[0] = "";
                PlayFile();
            }
            
            

        }

        private void Quit_Click(object sender, RoutedEventArgs e)
        {
            main_window.Close();
        }

        private void Play_Click(object sender, RoutedEventArgs e)
        {
            if (playing == 0)
            {
                Play.Visibility = Visibility.Collapsed;
                Pause.Visibility = Visibility.Visible;
                CPlay.Visibility = Visibility.Collapsed;
                CPause.Visibility = Visibility.Visible;
                
                Open_Click(sender, e);
                

            }
            else if (playing == 1)
            {
                Play.Visibility = Visibility.Visible;
                Pause.Visibility = Visibility.Collapsed;

                CPlay.Visibility = Visibility.Visible;
                CPause.Visibility = Visibility.Collapsed;
                player.Pause();
                playing = 2;
                System.Windows.Controls.Image img = new System.Windows.Controls.Image();
                Uri uri = new Uri("Play.png", UriKind.Relative);
                img.Source = new BitmapImage(uri);

                Button_Play.Content = img;
               
                dsp.Stop();
            }     


            else
            {
                Play.Visibility = Visibility.Collapsed;
                Pause.Visibility = Visibility.Visible;
                CPlay.Visibility = Visibility.Collapsed;
                CPause.Visibility = Visibility.Visible;


                PlayFile();
            }
       }

        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            if (playing != 0)
            {
                Play.Visibility = Visibility.Visible;
                Pause.Visibility = Visibility.Collapsed;
                CPlay.Visibility = Visibility.Visible;
                CPause.Visibility = Visibility.Collapsed;
                player.Stop();
                playing = 0;
                dsp.Stop();
                time_Slider.Value = 0;
                player.Close();
                System.Windows.Controls.Image img = new System.Windows.Controls.Image();
                Uri uri = new Uri("Play.png", UriKind.Relative);
                img.Source = new BitmapImage(uri);

                Button_Play.Content = img;

                
            }
        }


  /*      private void Pause_Click(object sender, RoutedEventArgs e)
        {
            if (playing != 0)
            {
                player.Pause();
                playing = 2;
                dsp.Stop();
            }           
        }

   
    */ 

        private void Rewind_Click(object sender, RoutedEventArgs e)
        {
            if (playing != 0)
            {
                player.Position = TimeSpan.Zero;
                time_Slider.Value = 0;
               
            }
        }

        private void menu_mute_Click(object sender, RoutedEventArgs e)
        {
            if (((MenuItem)(sender)).IsChecked == true)
            {
                mute = false;
                ((MenuItem)(sender)).IsChecked = false;
                player.Volume=oldVolume;
                BitmapImage img = new BitmapImage(new Uri("Volume.png", UriKind.Relative));
             
                System.Windows.Controls.Image img1 = new System.Windows.Controls.Image();
                img1.Source = img;
                
                volume_label.Content = img1;
                volume_TxtBlock.Foreground = new SolidColorBrush(Colors.White);
                volume_TxtBlock.Text = oldVolume.ToString();
            }
            else if (((MenuItem)(sender)).IsChecked == false)
            {
                ((MenuItem)(sender)).IsChecked = true;
                oldVolume = player.Volume;
                player.Volume = 0;
                BitmapImage img = new BitmapImage(new Uri("Mute.png", UriKind.Relative));

                System.Windows.Controls.Image img1 = new System.Windows.Controls.Image();
                img1.Source = img;

                volume_label.Content = img1;
                volume_TxtBlock.Foreground = new SolidColorBrush(Colors.Red);
                volume_TxtBlock.Text = "Mute";
                mute = true ;
            }
        }

        private void menu_IncAudio_Click(object sender, RoutedEventArgs e)
        {
            oldVolume = player.Volume;
            if (mute == true)
            {
                BitmapImage img = new BitmapImage(new Uri("Volume.png", UriKind.Relative));

                System.Windows.Controls.Image img1 = new System.Windows.Controls.Image();
                img1.Source = img;

                volume_label.Content = img1;

                volume_TxtBlock.Foreground = new SolidColorBrush(Colors.White);
                volume_TxtBlock.Text = vol.ToString();
                mute = false;
                menu_mute.IsChecked = false;
               
            }
            if(player.Volume <=.9)
            player.Volume += 0.01;

         
        }

        private void menu_DecAudio_Click(object sender, RoutedEventArgs e)
        {
            oldVolume = player.Volume;
            
            player.Volume -= 0.01;
            if (player.Volume < 0)
                player.Volume = 0;
        }

       

        void volume_slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            oldVolume = ((Slider)(sender)).Value;
            
            player.Volume = ((Slider)(sender)).Value;
            if (player.Volume < 0)
                player.Volume = 0;
            else if (player.Volume > 1)
                player.Volume = 1;
            volume_TxtBlock.Text = ((int)(volume_Slider.Value *100)).ToString();

        }

        private void player_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (e.Delta > 0)
            {
                
                if (player.Volume <= .9)
                {
                    oldVolume = player.Volume;
                    player.Volume += ((Double)e.Delta/1000);

                    if (mute == true)
                    {
                        BitmapImage img = new BitmapImage(new Uri("Volume.png", UriKind.Relative));

                        System.Windows.Controls.Image img1 = new System.Windows.Controls.Image();
                        img1.Source = img;

                        volume_label.Content = img1;

                        volume_TxtBlock.Foreground = new SolidColorBrush(Colors.White);
                        volume_TxtBlock.Text = vol.ToString();
                        mute = false;
                        menu_mute.IsChecked = false;

                    }
                }

            }
            else
            {
                if (player.Volume >= .1)
                {
                    oldVolume = player.Volume;
                    player.Volume += ((Double)e.Delta/1000);
                   if (mute == true)
                    {
                        BitmapImage img = new BitmapImage(new Uri("Volume.png", UriKind.Relative));

                        System.Windows.Controls.Image img1 = new System.Windows.Controls.Image();
                        img1.Source = img;

                        volume_label.Content = img1;

                        volume_TxtBlock.Foreground = new SolidColorBrush(Colors.White);
                        volume_TxtBlock.Text = vol.ToString();
                        mute = false;

                    }
                  
                }
            }


        }

       

       private void time_Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
       {
           if(slider_flag!=1)
            player.Position = TimeSpan.FromSeconds(((Slider)(sender)).Value);
        }

       private void player_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
       {
           if (playing != 0 && player.HasVideo == true)
           {
               if (e.ClickCount == 1 && dbl_click_flag == 1)
               {
                   this.Cursor = Cursors.None;
                   player.Focus();

               }
               if (e.ClickCount == 2 && dbl_click_flag == 0)
               {
                   dbl_click_flag = 1;

                   if (Math.Abs((video_ratio - screen_aspect_ratio)) < .5)
                   {

                       original_main_menu_height = main_menu.Height;
                       main_menu.Height = 0;
                       old_wstate = main_window.WindowState;
                       main_window.WindowState = WindowState.Normal;

                       old_wstyle = main_window.WindowStyle;
                       main_window.WindowStyle = WindowStyle.None;
                       IsTopmost = main_window.Topmost;
                       main_window.Topmost = true;
                       old_wstate = main_window.WindowState;
                       main_window.WindowState = WindowState.Maximized;
                       old_rmode = main_window.ResizeMode;
                       main_window.ResizeMode = ResizeMode.NoResize;
                       if (Environment.OSVersion.Version.Major >= 6)
                       {
                           System.Windows.Media.Color col = new System.Windows.Media.Color();
                           col.ScR = 255;
                           col.ScB = 255;
                           col.ScG = 255;
                           col.ScA = 255;
                           timeline_grid.Background = new SolidColorBrush(col);
                           col.ScR = 0;
                           col.ScB = 0;
                           col.ScG = 0;
                           col.ScA = 255;
                           timeline_textBox.Foreground = new SolidColorBrush(col);
                           media_player_SP.Background = new SolidColorBrush(col);
                       }
                      // player.Stretch = Stretch.Uniform; //Changed
                       //  player.StretchDirection = StretchDirection.Both;
                      
                       old_grid_height = ControlGrid.Height;
                       ControlGrid.Height = 0;
                      this.Cursor = Cursors.None;
                  
                       full_screen = true;
                   }
                   else if (Math.Abs(video_ratio - screen_aspect_ratio) > .5)
                   {

                       original_main_menu_height = main_menu.Height;
                       main_menu.Height = 0;
                       old_wstate = main_window.WindowState;
                       main_window.WindowState = WindowState.Normal;

                       old_wstyle = main_window.WindowStyle;
                       main_window.WindowStyle = WindowStyle.None;
                       IsTopmost = main_window.Topmost;
                       main_window.Topmost = true;
                       old_wstate = main_window.WindowState;
                       main_window.WindowState = WindowState.Maximized;
                       old_rmode = main_window.ResizeMode;
                       main_window.ResizeMode = ResizeMode.NoResize;

                      // player.Stretch = Stretch.Fill;  //Changed
                       //player.StretchDirection = StretchDirection.Both;
                       if (Environment.OSVersion.Version.Major >= 6)
                       {
                           System.Windows.Media.Color col = new System.Windows.Media.Color();
                           col.ScR = 255;
                           col.ScB = 255;
                           col.ScG = 255;
                           col.ScA = 255;
                           timeline_grid.Background = new SolidColorBrush(col);
                           col.ScR = 0;
                           col.ScB = 0;
                           col.ScG = 0;
                           col.ScA = 255;
                           timeline_textBox.Foreground = new SolidColorBrush(col);
                           media_player_SP.Background = new SolidColorBrush(col);
                       }
                       old_grid_height = ControlGrid.Height;
                       ControlGrid.Height = 0;

                       this.Cursor = Cursors.None;
                      
                   }

               }
               else if (e.ClickCount == 2 && dbl_click_flag == 1)
               {
                   full_screen = false;
                   main_menu.Height = original_main_menu_height;
                   main_window.WindowStyle = old_wstyle;
                   main_window.Topmost = IsTopmost;
                   main_window.WindowState = old_wstate;
                   main_window.ResizeMode = old_rmode;
                 //  player.Stretch = Stretch.Fill; //Changed
                   if (Environment.OSVersion.Version.Major >= 6)
                   {
                       System.Windows.Media.Color col = new System.Windows.Media.Color();
                       col.ScR = 0;
                       col.ScB = 0;
                       col.ScG = 0;
                       col.ScA = 255;
                       System.Windows.Media.Color col1 = new System.Windows.Media.Color();
                       col1.ScR = 255;
                       col1.ScG = 255;
                       col1.ScB = 255;
                       col1.ScA = 0;

                       timeline_grid.Background = new SolidColorBrush(col1);
                       timeline_textBox.Foreground = new SolidColorBrush(Colors.White);
                       media_player_SP.Background = new SolidColorBrush(col);

                   } ControlGrid.Height = old_grid_height;
                   this.Cursor = Cursors.Arrow;
                  
                   dbl_click_flag = 0;
               }
           }
           
       }
       private void small_screen()
       {
           if (dbl_click_flag == 1)
           {
               full_screen = false;
               main_menu.Height = original_main_menu_height;
               main_window.WindowStyle = old_wstyle;
               main_window.Topmost = IsTopmost;
               main_window.WindowState = old_wstate;
               main_window.ResizeMode = old_rmode;
               //player.Stretch = Stretch.Fill;
               //player.StretchDirection = StretchDirection.Both;
               if (Environment.OSVersion.Version.Major >= 6)
               {
                   System.Windows.Media.Color col = new System.Windows.Media.Color();
                   col.ScR = 0;
                   col.ScB = 0;
                   col.ScG = 0;
                   col.ScA = 0;

                   timeline_grid.Background = new SolidColorBrush(col);
                   col.ScA = 0;
                   media_player_SP.Background = new SolidColorBrush(col);
               }

               ControlGrid.Height = old_grid_height;
               this.Cursor = Cursors.Arrow;
               
               dbl_click_flag = 0;
           }
       }

       private void menu_help_about_Click(object sender, RoutedEventArgs e)
       {
           Window window = new Window();
           window.ResizeMode = ResizeMode.NoResize;
           window.Width = 300;
           window.Height = 300;
           window.Title = "About This Player";
           if (Environment.OSVersion.Version.Major >= 6)
           {

               System.Windows.Media.Color col = new System.Windows.Media.Color();
               col.ScA = 255;
               col.ScB = 255;
               col.ScR = 255;
               col.ScG = 255;
               window.Background = new SolidColorBrush(col);
               window.WindowStyle = WindowStyle.SingleBorderWindow;
               window.Icon = null;
           }
           StackPanel sp = new StackPanel();
           window.Content = sp;
           TextBlock tb = new TextBlock();
           tb.Text = "This is a Karanveer Singh Production";
           tb.FontWeight = FontWeights.Bold;
           window.Topmost = true;

           TextBlock newtb = new TextBlock();
           newtb.FontSize = 12;
           newtb.FontStyle = FontStyles.Italic;
           newtb.Text = "This is a project made by me \n:Karanveer Singh for my 6 weeks summer training.\n " + "\nAny unauthorised copying  of the code \nis  strictly punishable by Law.\n" + "This has been created using WPF 4.0 in C#\n" + "All Rights Reserved\n" + "VistaGlassHelper Code from \nPro WPF using C#";
           sp.Children.Add(tb);
           sp.Children.Add(newtb);
           window.ShowDialog();
         /*  if (Environment.OSVersion.Version.Major >= 6)
           {
               Windows.VistaGlassHelper.ExtendGlass(window, ((int)(window.Left)), ((int)(window.Left + main_window.Width)), ((int)(main_window.Top)), ((int)(main_window.Top + main_window.Height)));
           }
           */
       }

       private void player_MouseMove(object sender, MouseEventArgs e)
       {
           this.Cursor = Cursors.Arrow;
        //   timeline_grid.Visibility=Visibility.Visible;
         
       }

       private void End_Click(object sender, RoutedEventArgs e)
       {
           if (playing != 0)
           {
               player.Position = TimeSpan.FromSeconds(player.NaturalDuration.TimeSpan.TotalSeconds);
               time_Slider.Value = time_Slider.Maximum;

               if (!playlist.Equals("")&&playlist_count == playlist.Length )
               {
                   System.Windows.Controls.Image img = new System.Windows.Controls.Image();
                   Uri uri = new Uri("Play.png", UriKind.Relative);
                   img.Source = new BitmapImage(uri);

                   Button_Play.Content = img;


               }
               else
               {
                   System.Windows.Controls.Image img = new System.Windows.Controls.Image();
                   Uri uri = new Uri("Play.png", UriKind.Relative);
                   img.Source = new BitmapImage(uri);

                   Button_Play.Content = img;

               }
              
           }
       }

       private void main_window_PreviewKeyDown(object sender, KeyEventArgs e)
       {
           if (e.Key == Key.Space)
           {
               Play_Click(sender, e);
           }
           if (e.Key == Key.Right)
           {
               if ((e.KeyboardDevice.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
               {
                   player.Position += new TimeSpan(0, 1,0);
                   slider_flag = 1;
                   time_Slider.Value = player.Position.TotalSeconds;
                   slider_flag = 0;
               }
           }
           if (e.Key == Key.Left)
           {
               if ((e.KeyboardDevice.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
               {
                   player.Position -= new TimeSpan(0, 1, 0);
                   slider_flag = 1;
                   time_Slider.Value = player.Position.TotalSeconds;
                   slider_flag = 0;
               }
           }
           if (e.Key == Key.P)
           {
               if ((e.KeyboardDevice.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
               {
                   Play_Click(sender,e);    
               }
           }
           if (e.Key == Key.Q)
           {
               if ((e.KeyboardDevice.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
               {
                   Quit_Click(sender, e);
               }
           }
           if (e.Key == Key.O)
           {
               if ((e.KeyboardDevice.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
               {
                   Open_Click(sender, e);
               }
           }
           if (e.Key == Key.S)
           {
               if ((e.KeyboardDevice.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
               {
                   Stop_Click(sender, e);
               }
           }
           if (e.Key == Key.Add)
           {
            
               
               menu_IncAudio_Click(sender, e);
               
           }
           if (e.Key == Key.Subtract)
           {
               menu_DecAudio_Click(sender,e);
           }
           if (e.Key == Key.M)
           {
               player.Volume = 0;
           }
           if (e.Key == Key.Up)
           {
               if ((e.KeyboardDevice.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
               {

                  
                   menu_IncAudio_Click(sender, e);
               }
              
           }
           if (e.Key == Key.Down)
           {
               if ((e.KeyboardDevice.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
               {
                   menu_DecAudio_Click(sender, e);
               }

           }
           if (e.Key == Key.Escape)
           {
               if (full_screen == true)
               {

                   small_screen();
                   
                  
                   
                   
               }
           }

           if (e.Key == Key.L)
           {
               if ((e.KeyboardDevice.Modifiers & ModifierKeys.Control )== ModifierKeys.Control)
               {
                   Window playlist_window = new Window();
                   playlist_window.Height = 400;
                   playlist_window.Width = 400;
                   playlist_window.Title = "Playlist";
                   
                   ListBox playlist_listbox = new ListBox();
                   playlist_listbox.SelectionMode = SelectionMode.Single;
                   playlist_listbox.SelectionChanged += new SelectionChangedEventHandler(playlist_listbox_SelectionChanged);
                   int count = 0;
                   while (count <playlist.Length)
                   {
                       ListBoxItem lb_item = new ListBoxItem();
                       lb_item.Content = playlist[count] ;
                       playlist_listbox.Items.Add(lb_item);
                       count++;
                   }
                   ScrollViewer scroll = new ScrollViewer();
                   scroll.Content=playlist_listbox;
                   playlist_window.Content=scroll;
                   if (main_window.IsTopmost)
                   {
                       main_window.Topmost = false;
                       playlist_topmost = true;
                       playlist_window.Topmost = true;

                   }
                   playlist_window.Closed += new EventHandler(playlist_window_Closed);
                   playlist_window.ShowDialog();
                  

               }

           }

       }

       void playlist_window_Closed(object sender, EventArgs e)
       {
           //throw new NotImplementedException();
           if (playlist_topmost)
           {
           
               main_window.Topmost = true;
           }    
           
       }

       void playlist_listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
       {
           //throw new NotImplementedException();
           ListBox lb = sender as ListBox;
           if (!playlist[0].Equals(""))
           {
               playlist_count = lb.Items.IndexOf(lb.SelectedItem);
               pathname = playlist[playlist_count].ToString();
               player.Source = new Uri(pathname);
               player.LoadedBehavior = MediaState.Manual;
               playlist_count++;
               PlayFile();

           }
          
       }
       private void ThumbInfoOpen()
       {
           OpenFileDialog ofd = new OpenFileDialog();
           ofd.Filter = "All Files (*.*) |*.*|Video Files (*.AVI;*.MPG;*.MPEG;*.DIVX;*.XVID;*.MKV;*.MP4;*.3GP)|*.AVI;*.MPG;*.MPEG;*.DIVX;*.XVID;*.MKV;*.MP4;*.3GP |Audio Files(*.WAV;*.MP3;*.WMA;*.CDA)|*.WAV;*.MP3;*.WMA;*.CDA";
           ofd.ShowDialog();
           pathname = ofd.FileName;
           if (!pathname.Equals(""))
           {

               player.Source = new Uri(pathname);


               player.LoadedBehavior = MediaState.Manual;
               PlayFile();
           }
          


       }
       private void ThumbButtonInfo_Play_Click(object sender, EventArgs e)
       {
           if (playing == 0)
               ThumbInfoOpen();
           else
           {

               PlayFile();
           }
       }

       private void ThumbButtonInfo_Pause_Click(object sender, EventArgs e)
       {
           if (playing != 0)
           {
               player.Pause();
               playing = 2;
               dsp.Stop();
               System.Windows.Controls.Image img = new System.Windows.Controls.Image();
               Uri uri = new Uri("Play.png", UriKind.Relative);
               img.Source = new BitmapImage(uri);

               Button_Play.Content = img;
           }
       }
       private void ThumbButtonInfo_Stop_Click(object sender, EventArgs e)
       {
           if (playing != 0)
           {
               player.Stop();
               playing = 0;
               dsp.Stop();
               time_Slider.Value = 0;
               player.Close();
               System.Windows.Controls.Image img = new System.Windows.Controls.Image();
               Uri uri = new Uri("Play.png", UriKind.Relative);
               img.Source = new BitmapImage(uri);

               Button_Play.Content = img;

           }
       }
       private void MakeTransparent()
       {
           System.Windows.Media.Color col = new System.Windows.Media.Color();
           col.ScA=0;
           col.ScB=0;
           col.ScG=0;
           col.ScR=0;
           ControlGrid.Background=new SolidColorBrush(col);
          Windows.VistaGlassHelper.ExtendGlass(main_window, ((int)(main_window.Left)), ((int)(main_window.Left + main_window.Width)), ((int)(main_window.Top + main_window.Height-ControlGrid.Height)), ((int)(main_window.Top + main_window.Height)));
          // Windows.VistaGlassHelper.ExtendGlass(main_window, ((int)main_window.Left), ((int)(main_window.Left + main_window.Width)),((int)(main_window.Top)),100);
        
       }
       private void main_window_Loaded(object sender, RoutedEventArgs e)
       {
           if (Environment.OSVersion.Version.Major >= 6)
           {   
               System.Windows.Media.Color col = new System.Windows.Media.Color();
               col.A = 0;
               col.B = 255;
               col.G = 255;
               col.R = 255;

               main_menu.Background = new SolidColorBrush(Colors.Transparent);

               MakeTransparent();
           }
           else
           {
               
               
               main_menu.Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(255,255,255));
           }
           old_timeline_height = timeline_grid.Height;

       }

       private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
       {
           if (mute == false)
           {
               vol = volume_Slider.Value;
               volume_Slider.Value = 0;
               volume_TxtBlock.Foreground = new SolidColorBrush(Colors.Red);
               volume_TxtBlock.Text = "Mute";
               mute = true;
               BitmapImage img = new BitmapImage(new Uri("Mute.png",UriKind.Relative));
               
               System.Windows.Controls.Image img1 = new System.Windows.Controls.Image();
               img1.Source= img;

               volume_label.Content = img1;
               menu_mute.IsChecked = true;

           }
           else
           {
               volume_Slider.Value = vol;
               
               volume_TxtBlock.Foreground = new SolidColorBrush(Colors.White);
               volume_TxtBlock.Text=(vol*100).ToString();
               mute = false;
               BitmapImage img = new BitmapImage(new Uri("Volume.png", UriKind.Relative));

               System.Windows.Controls.Image img1 = new System.Windows.Controls.Image();
               img1.Source = img;

               volume_label.Content = img1;
               menu_mute.IsChecked = false;
           }
       }

       private void media_player_SP_Drop(object sender, DragEventArgs e)
       {
           
               playlist = (string[])e.Data.GetData(DataFormats.FileDrop, true);
               playlist_count = 0;
              
           
         /*  else 
           {
               string[] temp = (string[])e.Data.GetData(DataFormats.FileDrop, true);
               string[] temp1 = new string[playlist.Length + temp.Length];
               for (int i = 0; i < playlist.Length; i++)
               {
                   temp1[i] = playlist[i];
               }
               for (int i = playlist.Length; i < (playlist.Length + temp.Length); i++)
               {
                   temp1[i] = temp[i-playlist.Length];
               }
               playlist=temp1;
               playlist_count = playlist.Length;

              
           }
          * */
               if (playlist[0].Contains("mp3") || playlist[0].Contains("mp4") || playlist[0].Contains("MP4") || playlist[0].Contains("MP3") || playlist[0].Contains("AVI") || playlist[0].Contains("avi") || playlist[0].Contains("divx") || playlist[0].Contains("DIVX") || playlist[0].Contains("xvid") || playlist[0].Contains("XVID") || playlist[0].Contains("wmv") || playlist[0].Contains("WMV") || playlist[0].Contains("MKV") || playlist[0].Contains("mkv") || playlist[0].Contains("wmv"))
               {
                   if (playlist.Length != 0)
                   {
                       pathname = playlist[0].ToString();
                       main_window.Title = pathname;
                       player.Source = new Uri(pathname);
                       player.LoadedBehavior = MediaState.Manual;
                       playlist_count++;
                       PlayFile();
                   }
               }
       }

       private void time_Slider_MouseEnter(object sender, MouseEventArgs e)
       {
           timeline_grid.Visibility=Visibility.Visible;

       }

     
     /*  private void time_Slider_MouseDoubleClick(object sender, MouseButtonEventArgs e)
       {

           
           System.Windows.Point pt = new System.Windows.Point();
           pt = e.GetPosition(time_Slider as IInputElement);
           Me
       }
        */
   /*   private void menu_subtitle_add_Click(object sender, RoutedEventArgs e)
       {
           OpenFileDialog ofd = new OpenFileDialog();
           ofd.Title = "Open Subtitle";
           ofd.Filter = "SRT Files(*.srt)|*.srt"
                        +"| All Files(*.*)|*.*";
           
           ofd.ShowDialog();
           if (!ofd.FileName.Equals(""))
           {
               StreamReader read_stream = new StreamReader(ofd.FileName);
               while (!read_stream.EndOfStream)
               {
                   int val;
                   int[] start_time = new int[3];
                  string str= Console.ReadLine();
                  if (Int32.TryParse(str, out val))
                  {
                     string times = Console.ReadLine();
                     for (int i = 0; i < times.Length; i++)
                     {
                         if (times[i] == '-')
                             break;
                         else
                         {
                             if (times[i] == ':' || times[i] == ',')
                                 continue;
                             else
                             {
                                     

                             }
                         }
                     }
                           
                  }
                  else
                  {


                  }
                 
               }

     
           }
      


       }

    */



       



        
        
      
 
        

       

      
    }
}
